var touch;
var chsh;
function makePOSTRequest(url, response, postdata) {
	var params = {};
	params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
	params[gadgets.io.RequestParameters.POST_DATA] = postdata;
	params[gadgets.io.RequestParameters.HEADERS] = {
		'Content-Length': postdata.length,
		'Content-Type': 'application/x-www-form-urlencoded'
	}
   gadgets.io.makeRequest(url, response, params);
};




resSearch = function (obj) {
	var html = obj.text.replace( /[\s\n\t\f]/gm, ''); //復帰文字、タブ、改行文字、ラインフィードの削除
	var tables = [];
	
	html.replace( new RegExp( '(<table[^>]*>(.*?)</table>)', 'gm'), function(){
			tables.push( arguments[0]);
	        return "";
	    }
	);
	
	var jobs = [];
	
	for( var i=0; i<tables.length; i++){
		var table = tables[ i];
		if( table.match( /求人番号/)){
			var trs = []
			table.replace( new RegExp( '(<tr[^>]*>(.*?)</tr>)', 'gm'), function(){
				trs.push( arguments[0]);
		        return "";
		    });
			
			var l = trs.length;
			
			for( var j=0; j<l; j++){
				var tr = trs[ j];
				var keys = [ 'new', 'no', 'id|url', 'occupation', 'employment-form|wage', 'working-hours|holiday|five-day-workweek', 'industry', 'wayside|workplace', 'pubdate'];
				var job = "";
				tr.replace( new RegExp( '(<td[^>]*>(.*?)</td>)', 'gm'), function(){
					job += '\n--' +keys.shift() +arguments[2].replace( /<div[^>]*?>|<\/div>/g, '').replace( /<br[^>]*?>/g, '<br>');
			        return "";
			    });
				if( job != ""){
					jobs.push( job);
				}
			}
			break;
		}
	}
	
	$( '#output').text( jobs.join( '\n'));
	
};

var data = [
	"kyushokuNumber1=",
	"kyushokuNumber2=",
	"kyushokuUmu=2",
	"kyujinShurui=1",
	"gekkyuKagen=",
	"teate=1",
	"kiboShokushu=",
	"todofuken1=",
	"todofuken2=",
	"todofuken3=",
	"chiku=",
	"nenrei=",
	"kiboSangyo=",
	"commonSearch=%E6%A4%9C%E7%B4%A2",
	"screenId=B01020",
	"action=",
	"codeAssistType=",
	"codeAssistKind=",
	"codeAssistCode=",
	"codeAssistItemCode=",
	"codeAssistItemName=",
	"codeAssistDivide=",
	"xab_vrbs=commonNextScreen%2CcommonSearch%2CdetailSearchButton"
].join( '&')


gadgets.util.registerOnLoadHandler(
	function(){
		makePOSTRequest("https://www.hellowork.go.jp/servicef/B01020.do", resSearch, data);
	}
);